package com.lagou.filter;

import org.I0Itec.zkclient.ZkClient;
import redis.clients.jedis.Jedis;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName MonitorTp
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/4/13
 * @Version 1.0.0
 */
public class MonitorTp {

    private static final String  namespace = "/dubbo";
    private static Method[] methods=null;
    private static String[] methodNames=null;

    static {
        ZkClient  zkClient = new ZkClient("127.0.0.1:2181", 5000);
        zkClient.setZkSerializer(new MyZkSerializer());

        //初始化在线连接
        List<String> list = zkClient.getChildren(namespace);
        if(null != list && list.size()!=0){
            for (int j = 0;j < list.size()-1;j++) {
                String interfaceClass = list.get(j);
                try {
                    Class<?> aClass = Class.forName(interfaceClass);
                    methods = aClass.getMethods();
                    if(null != methods){
                        methodNames = new String[methods.length];
                        for (int i=0;i<methods.length;i++) {
                            methodNames[i]=methods[i].getName();
                        }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void clear(){
        methods = null;
        methodNames = null;
    }

    /**
     * 开始监控
     */
    public void tpMonitor(){
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                Jedis jedis = JedisPoolUtil.getJedis();
                try {
                for (String methodName : methodNames) {
                    //获取方法全部值
                    Set<String> keys = jedis.keys("TP:" + methodName + "*");
                    int size = keys.size();
                    ArrayList<Integer> strings = new ArrayList<>();
                    keys.forEach(k->{
                        int s = Integer.parseInt(jedis.get(k));
                        strings.add(s);
                    });
                    int[] ints = new int[strings.size()];
                    for(int i = 0; i < strings.size();i++){
                        ints[i] = strings.get(i);
                    }
                    synchronized (ints){
                        Arrays.sort(ints);
                    }
                    //向下取整，获取下标位置
                    double ceil = Math.floor(size * 0.90);
                    System.out.println(methodName+" tp90耗时时间："+ints[(int)ceil]+"请求总数为："+size);
                    double ceil99 = Math.floor(size * 0.99);
                    System.out.println(methodName+" tp99耗时时间："+ints[(int)ceil99]+"请求总数为："+size);
                }


                }catch (Exception e){
                    e.getStackTrace();
                }finally {
                    jedis.close();
                }
            }

        },5,5, TimeUnit.SECONDS);

    }
}
